package com.ruoyi.back.service.v3.impl

import cn.hutool.core.bean.BeanUtil
import cn.hutool.core.date.DateUtil
import cn.hutool.core.util.StrUtil
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import com.ruoyi.app.common.*
import com.ruoyi.back.domain.UserMemberOrder
import com.ruoyi.back.domain.v3.CheckStatusEnum
import com.ruoyi.back.domain.v3.MyRecord
import com.ruoyi.back.domain.v3.Recommend
import com.ruoyi.back.domain.v3.dto.RecommendRecordSearchDto
import com.ruoyi.back.domain.v3.vo.RecommendNum
import com.ruoyi.back.domain.v3.vo.RecommendRecordListVo
import com.ruoyi.back.domain.v3.vo.UserRecordListVo
import com.ruoyi.back.domain.v3.vo.UserRecordNum
import com.ruoyi.back.domain.vo.UserMemberOrderListVo
import com.ruoyi.back.enums.PayStatusEnum
import com.ruoyi.back.mapper.RecommendMapper
import com.ruoyi.back.service.UserRecordService
import com.ruoyi.back.service.v3.MyRecordService
import com.ruoyi.back.service.v3.RecommendService
import com.ruoyi.common.core.page.TableSupport
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import java.util.*

@Service
class RecommendServiceImpl : RecommendService, ServiceImpl<RecommendMapper, Recommend>() {

    @Autowired
    private lateinit var myRecordService: MyRecordService

    override fun recommendRecordList(recommendRecordSearchDto: RecommendRecordSearchDto): Page<RecommendRecordListVo> =

        TableSupport.buildPageRequest().let { pageDomain ->

            page(
                Page(
                    pageDomain.pageNum.toLong(),
                    pageDomain.pageSize.toLong()
                ), KtQueryWrapper(Recommend::class.java)
                    .eq(
                        StrUtil.isNotBlank(recommendRecordSearchDto.userInfo),
                        Recommend::username,
                        recommendRecordSearchDto.userInfo
                    )
                    .or().eq(
                        StrUtil.isNotBlank(recommendRecordSearchDto.userInfo),
                        Recommend::userInfoId,
                        recommendRecordSearchDto.userInfo
                    )
                    .like(StrUtil.isNotBlank(recommendRecordSearchDto.prompt), Recommend::descText, recommendRecordSearchDto.prompt)
                    .eq(recommendRecordSearchDto.type != null, Recommend::type, recommendRecordSearchDto.type)
                    .gt(StrUtil.isNotBlank(recommendRecordSearchDto.startRecommendTime), Recommend::createTime, recommendRecordSearchDto.startRecommendTime.date())
                    .lt(StrUtil.isNotBlank(recommendRecordSearchDto.endUpRecommendTime), Recommend::createTime, recommendRecordSearchDto.endUpRecommendTime.date())
                    .orderByDesc(Recommend::createTime)
            )
                .let { recommendPage ->
                    Page<RecommendRecordListVo>().apply {
                        BeanUtil.copyProperties(recommendPage, this)
                        records = recommendPage.records.takeIf { it.isNotEmpty() }
                            ?.run {
                                map {
                                    RecommendRecordListVo().apply {
                                        BeanUtil.copyProperties(it, this)
                                        recommendTime = it.createTime?.format()
                                    }
                                }
                            }
                    }
                }
        }

    override fun checkRecommendRecord(id: Long, checkType: Int): Data<Boolean> =
        Ok("审核成功", getById(id)?.run {
            updateById(this.apply { this.checkStatus = checkType })
            myRecordService.getById(this.recordId)?.run {
                myRecordService.updateById(this.apply {
                    recommendStatus = checkType
                    shelves = true
                    shelvesTime = Date()
                })
            }
        })

    override fun recommendRecordNum(): Data<RecommendNum> =
        Ok("获取成功", RecommendNum().apply {
            preCheckNum = count(KtQueryWrapper(Recommend::class.java)
                .eq(Recommend::checkStatus, CheckStatusEnum.PRE_CHECK.type))
            checkSuccessNum = count(KtQueryWrapper(Recommend::class.java)
                .eq(Recommend::checkStatus, CheckStatusEnum.CHECK_PASS.type))
            checkFailNum = count(KtQueryWrapper(Recommend::class.java)
                .eq(Recommend::checkStatus, CheckStatusEnum.CHECK_FAIL.type))
        })
}